#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os


# list操作 类似数组？
classmate = ['dean', 'alice', 'えりな']
print("初始", len(classmate), classmate)
classmate.append("爱丽丝")
print("追加", len(classmate), classmate)
classmate.insert(2, "小林")
print("插入", len(classmate), classmate)
classmate.pop()
print("移除", len(classmate), classmate)
classmate.pop(1)
print("移除", len(classmate), classmate)
classmate.sort()
print("排序", classmate)
s = ["a", 'v', 111, ['111', 1.1], True]
print("多维", len(s), s, s[3][1])
print("切片", classmate[:2], classmate[1:2],
      classmate[-2:], classmate[-3:-2])  # 切片正从0开始，倒从-1开始
sliceList = list(range(100))
print("切片2", sliceList[30:60:2], sliceList[20::6])  # 每隔几个取  字符串切片仍是字符串
# tuple，list常量形式，无法被修改 但其指向的对象（或数组）本身可以修改
tuple = (1, 23, "111")
print(len(tuple), tuple, tuple[:2])  # 可切片


# dict 即map
dic = {"dean": "main", "alice": "first", "爱丽丝": "second", "小林": "slave"}
print(dic["alice"])
print("alice1" in dic)  # 字段检查
print(dic.get("alice1", "不存在"))
dic.pop("小林")
print(dic)
dic[(1, 2, 3)] = (1, 2, 3)  # dict的键必须为不可变的量
print(dic)
# dic[(1,[2,3])] = (1,[2,3]) #list不可作为键使用

# set 无序 无重复元素
s = set([1, 2, 5])
s.add(4)
s.remove(5)
print(s)

# 列表生成式
print([str(x) + m + n for x in range(10) if x %
       2 == 0 for m in 'ABCDE' for n in 'XYZMN'])

print([d for d in os.listdir('.')])
# 练习##########
L1 = ['Hello', 'World', 18, 'Apple', None]
print([d.lower() for d in L1 if isinstance(d, str)])


# 生成器 generator
G1 = (str(x) + m + n for x in range(10) if x %
      2 == 0 for m in 'ABCDE' for n in 'XYZMN')
print(G1, next(G1), next(G1))
for g in G1:
    print(g)


def fib(n):
    a, b, i = 0, 1, 0
    while i <= n:
        yield b
        a, b = b, b + a
        i = i + 1
    return "diva"


for f in fib(8):
    try:
        print(f)
    except StopIteration as s:
        print("函数的返回值为%s" % s.value)
# 练习:杨辉三角#########


def yh():
    yhL = [1]
    while True:
        yield yhL
        yhL.append(0)
        yhL = [yhL[i - 1] + yhL[i] for i in range(len(yhL))]


# yh = yh()
# print(next(yh),next(yh),next(yh))
n = 0
for t in yh():
    print(t)
    n = n + 1
    if n == 10:
        break
# 练习:杨辉三角自写方法#########


def yh2(m, n):
    if(n == 0 or m == n):
        return 1
    return yh2(m - 1, n - 1) + yh2(m - 1, n)


for x in range(10):
    print([yh2(x, n) for n in range(x + 1)])
